define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {
	
	
	
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xorder/index' + location.search,
                    add_url: 'xorder/add',
                    edit_url: 'xorder/edit',
                    del_url: 'xorder/del',
                    multi_url: 'xorder/multi',
                    import_url: 'xorder/import',
                    table: 'xorder',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'title', title: __('Title'), operate: 'LIKE',operate: false},
                        //{field: 'sale_en_name', title: __('Sale_en_name'), operate: 'LIKE'},
                        //{field: 'sale_product_id', title: __('Sale_product_id')},
                        {field: 'total_amount',  title: '合计',operate: false,formatter:function (value, row, index) {
                                return value/100;
							 }},
                        {field: 'reduce_amount', title: '优惠',operate: false,formatter:function (value, row, index) {
                                return value/100;
                            }},
						{field: 'actual_amount', title: '实付',operate: false,formatter:function (value, row, index) {
                                return value/100;
                            }},
                        //{field: 'user_id', title: __('User_id')},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
						{field: 'cdk', title: __('Cdk'), operate: 'LIKE'},
                        {field: 'paytype', title:'支付',operate: false},
                        {field: 'paymethod', title: '方式',searchList: {'alipay': __('支付宝'), 'wechat': __('微信')}},
                        {field: 'paystatus', title: '状态',searchList: {'1': __('已支付'), '0': __('未支付')}},
                        {field: 'source', title: __('Source'), operate: 'LIKE'},
                        {field: 'kf', title: __('Kf'), operate: 'LIKE'},
                        
                        {field: 'remark', title: __('Remark'), operate: 'LIKE', editable: true},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xorder/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xorder/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xorder/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
			$('.btn-sendCDk').on('click',function(){
				var cdk = $('#c-cdk').val();
				var xid = $(this).attr('data-xid');
				Fast.api.ajax({
					url: "xorder/sendCDK",
					type: "POST",
					data:{
						cdk:cdk,xid:xid
					},
				}, function (data, ret) {
					Toastr.success(ret.msg)
					//window.location.reload();
					// Controller.api.refresh(table, name);
					return false;
				}, function (data, ret) {
					Toastr.error(ret.msg)
					return false;
				})
			})
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
